robotbattlefandomcom-20200213-history
Radio 101
Radio is the method used to transmit data between robots. You can use this tutorial directly after finishing the Robot Battle main tutorial. But a knowledge of arrays and property values and some RB experience are a bonus. By the end of this tutorial you should: * Be able to transmit radio messages * Be able to receive and process radio messages * Understand the dangers of Radio Sending Messages Sending messages is easy. All you need to understand is the command: RadioSend(Channel, Message) Channel: This is the channel on which the message will be send. The receiving robot must be listening on the correct channel to receive it. (More on this later.) This channel can be 0-9. Channel 0 is the special public channel. Any messages transmitted on channel 0 can be received by any team and any robot regardless of what channel it is listening to. Message: This is the variable that will be transmitted. Any array or property values will also be sent. This is useful for transmitting lots of data at once. For Example: Msg = 42 Msg1 = "Array Value" Msg.Prop = "Property Value" Msg.my_energy = _energy RadioSend(4, Msg) In this example all four values are transmitted. About RadioSend * You can send whenever you want, but most robots do it in the core or after important events * You can only use the RadioSend command 20 times in one turn. * When teams are on, messages sent by other teams will arrive as "999999". (Except public messages.) Receiving Messages Be on the right channel In order to receive a message you must be listening to the channel it is sent on. You can change the channel you are listening to with the command. RadioChannel(Channel) Where Channel is 1-9. You can choose this channel in advance and simply make sure you transmit and receive on the same channel. Advanced Methods: Another option is to alter the channel based on a formula or get your robots to pick a random channel and let the rest of the team know via public channel 0. Knowing when you have a message You will know when you have a new message because the variable is set to true. You can setup and event that is triggered by _radio using the command. For example: Init{ RegRadio( GotRadio, 1) } GotRadio{ //Your Radio routine here } Advanced Method: You can also just check the value of _radio in your core. Remember to clear it afterwards! Getting the message Receiving messages is all about understanding the variable. So first here is a breakdown of the _radiocomm variable: _radiocomm The root value is the number of messages you have received _radiocommn Contains the channel the n-th message was sent on _radiocommn._msg Is the variable that was transmitted for the n-th message _radiocommn._bearing Is the bearing to the sender of the n-th message For example if _radiocomm is 3, the messages we have received are _radiocomm0, _radiocomm1 and _radiocomm2. Where _radiocomm0 is the oldest message. Also, _radiocomm0 is the channel the oldest message was sent on; _radiocomm0._msg is the oldest message sent. Don't worry about _bearing for now. So we know how many messages we have received and we can access them via an array. We can go through each message using a while loop: while (i < _radiocomm) //If it's value is 999999 then ignore the encrypted message if (_radiocommi._msg != 999999) // _radiocommi._msg is the message! endif i = i + 1 //increase the value of i for the next loop endw clear (_radiocomm) //Once we have processed the messages, clear them This loop will go through each received message, in order of oldest to newest. If your message contained array or property values you can access them in the usual way: _radiocommn._msg1 //Example Array Value _radiocommn._msg.prop //Example Property Value Alternatively you can use the command so you can access the message more easily copy( msg, _radiocommi._msg ) Dangers of Radio As well as having massive advantages, radio can also have disadvantages. Therefore it is very important that you are careful when implementing a radio routine. Giving away your bearing You should note that whenever you send a message, you also reveal your bearing in relation to the receiving robot. This happens even if you are not on the same team and can't decode the message itself. While this doesn't give away your precise location, it could be used to track you. Making sure it is a message you sent The most obvious danger is processing a message that wasn't sent by your own robots. This message may be an attempt to compromise your radio routine or it may simply be an opposing robots's normal communication. Such a message will not be structured in the same way as your messages, so processing it could cause errors. The simplest solution to this is to "mark" your own messages so you can identify them. There are several methods to do this. For example, you can set the root of the message to a specific string or your message. Alternatively you could contain a specific property and then check for it using the command. Whatever method you decide to use you must protect your 'secret' to avoid radio attacks. You can do this by scrambling your robot, or simply changing your secret before every contest. Avoiding Errors No matter how hard you try some rogue messages might slip through the net. If your routine isn't protected fully, you may wind up getting Unknown variable or Invalid value errors. The former is caused by trying to access a variable that does not exist; the latter is caused by treating a number like a string or vice-versa. msg = 0 msg.string = "This is a string" msg.number = 42 print(msg.missing) //Produces an error a = (msg.string / 4) //Produces an error strrev( msg.number ) //Produces an error You can check if a property exists by using the command. You can determine if a given variable is a number or a string using and . msg = 0 msg.string = "This is a string" msg.number = 42 hasprop(msg, "missing") if (_result) print(msg.missing) endif isnum(msg.string) if (_result) a = (msg.string / 4) endif isstr(msg.number) if (_result) strrev( msg.number ) endif //This code produces no errors, but does nothing else Conclusion That should be enough to get your first radio robot off the ground. As always, if you get stuck post your problem on the registry. Try to be as specific as possible and cite this document if necessary. Category:Tutorials